package com.talking.view;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.talking.App;
import com.talking.adapter.VertifyAdapter;
import com.talking.bean.VertifyBean;
import com.talking.databinding.ActivityVeritymsgBinding;
import com.talking.presenter.FriendPresenter;
import com.talking.presenter.MessagePresenter;
import com.talking.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class VerityMsgActivity extends AppCompatActivity {
    private final List<VertifyBean> data=new ArrayList<>();
    private VertifyAdapter adp;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityVeritymsgBinding binding = ActivityVeritymsgBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        adp=new VertifyAdapter(this, data, vb -> {
            String message = App.sp.getString("message", "");
            if (message.equals("")){
                refresh();
            }else {
                try {
                    JSONArray ja = new JSONArray(message);
                    int tempIndex = -1;
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.getJSONObject(i);
                        if (jo.getString("createTime").equals(vb.createTime)){
                            tempIndex=i;
                        }
                    }
                    if (tempIndex!=-1){
                        JSONObject jsonObject=ja.getJSONObject(tempIndex);
                        MessagePresenter.deleteMsg(jsonObject.getString("id"), new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                e.printStackTrace();
                            }
                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                               //删除成功
                            }
                        });
                        ja.remove(tempIndex);
                    }
                    @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                    edit.putString("message",ja.toString());
                    edit.apply();
                    refresh();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, vb -> {
            String message = App.sp.getString("message", "");
            if (message.equals("")){
                refresh();
            }else {
                try {
                    JSONArray ja = new JSONArray(message);
                    int tempIndex = -1;
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.getJSONObject(i);
                        if (jo.getString("createTime").equals(vb.createTime)){
                            tempIndex=i;
                        }
                    }
                    if (tempIndex!=-1){
                        JSONObject jsonObject=ja.getJSONObject(tempIndex);
                        String remarkMe=new JSONObject(jsonObject.getString("content")).getString("remarks");

                        EditText edit=new EditText(this);
                        edit.setHint("请输入备注");
                        AlertDialog.Builder dialog=new AlertDialog.Builder(this);
                        int finalTempIndex = tempIndex;
                        dialog.setMessage("添加备注").setView(edit).setPositiveButton("确定", (dialog1, which) -> {
                            try {
                                FriendPresenter.addFriend(App.sp.getString("account", ""), jsonObject.getString("senderAccount"), remarkMe, edit.getText().toString(), new Callback() {
                                            @Override
                                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                                e.printStackTrace();
                                            }
                                            @Override
                                            public void onResponse(@NotNull Call call, @NotNull Response response) {
                                                runOnUiThread(() -> {
                                                    try {
                                                        Utils.toast( response.body().string());
                                                        MessagePresenter.deleteMsg(jsonObject.getString("id"), new Callback() {
                                                            @Override
                                                            public void onFailure(@NotNull Call call1, @NotNull IOException e) {
                                                                e.printStackTrace();
                                                            }
                                                            @Override
                                                            public void onResponse(@NotNull Call call1, @NotNull Response response1) {
                                                                //删除成功
                                                            }
                                                        });
                                                        refresh();
                                                    } catch (Exception e) {
                                                        e.printStackTrace();
                                                    }
                                                });
                                            }
                                        });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            ja.remove(finalTempIndex);
                            @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit1 = App.sp.edit();
                            edit1.putString("message",ja.toString());
                            edit1.apply();
                        }).setNeutralButton("取消",null).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        binding.verityList.setAdapter(adp);
        refresh();
    }
    private void refresh() {
        data.clear();
        String msg = App.sp.getString("message", "");
        if (!msg.equals("")){
            try {
                JSONArray ja = new JSONArray(msg);
                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = ja.getJSONObject(i);
                    if (jo.getString("type").equals("加好友")){
                        data.add(new VertifyBean(jo.getString("content"),jo.getString("senderAccount"),jo.getString("createTime")));
                    }
                }
                adp.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}